﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ARR.UI.ViewModels;
using ARR.Entities;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.IO;
using System.Xml;
using System.Windows.Markup;
using System.ComponentModel;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for RangeReviewView.xaml
    /// </summary>
    public partial class RangeReviewView : UserControl
    {
        public RangeReviewView()
        {
            InitializeComponent();
            
            this.SetupsGV.RowLoaded += new EventHandler<RowLoadedEventArgs>(SetupsGV_RowLoaded);
            this.BenchmarksGV.RowDetailsVisibilityChanged += new EventHandler<GridViewRowDetailsEventArgs>(BenchmarksGV_RowDetailsVisibilityChanged);
        }

        void BenchmarksGV_RowDetailsVisibilityChanged(object sender, GridViewRowDetailsEventArgs e)
        {
            RadGridView grid = e.DetailsElement as RadGridView;

            if (grid != null)
            {
                RangeReviewBenchmark bench = e.Row.DataContext as RangeReviewBenchmark;

                if (bench != null)
                {
                    if (bench.HistoryGroup != null && bench.HistoryGroup.Members != null)
                    {
                        GetHistoryGrid(grid);
                        grid.ItemsSource = bench.HistoryGroup.Members;
                    }
                    else if (bench.StoreGroup != null && bench.StoreGroup.Members != null)
                    {
                        GetStoresGrid(grid);
                        grid.ItemsSource = bench.StoreGroup.Members;
                    }
                }
            }
        }

        private void GetStoresGrid(RadGridView grid)
        {
            grid.AutoGenerateColumns = false;
            grid.Columns.Clear();
            
            Telerik.Windows.Controls.GridViewDataColumn codeCol = new Telerik.Windows.Controls.GridViewDataColumn();
            codeCol.Header = "Codigo";
            codeCol.DataMemberBinding = new Binding("Store.Code");

            Telerik.Windows.Controls.GridViewDataColumn nameCol = new Telerik.Windows.Controls.GridViewDataColumn();
            nameCol.Header = "Nombre";
            nameCol.DataMemberBinding = new Binding("Store.Name");


            grid.Columns.Add(codeCol);
            grid.Columns.Add(nameCol);            
        }

        private void GetHistoryGrid(RadGridView grid)
        {
            grid.AutoGenerateColumns = false;
            grid.Columns.Clear();

            Telerik.Windows.Controls.GridViewDataColumn titleCol = new Telerik.Windows.Controls.GridViewDataColumn();
            titleCol.Header = "Titulo";
            titleCol.DataMemberBinding = new Binding("History.Title");

            Telerik.Windows.Controls.GridViewDataColumn countryCol = new Telerik.Windows.Controls.GridViewDataColumn();
            countryCol.Header = "Pais";
            countryCol.DataMemberBinding = new Binding("History.CountryName");

            grid.Columns.Add(titleCol);
            grid.Columns.Add(countryCol);
        }

        private object currentItem;
        private CheckBox currentCheckBox;

        void SetupsGV_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            RangeReviewSetup setup = e.DataElement as RangeReviewSetup;
            if (setup != null)
            {
                for (int i = 8; i < e.GridViewDataControl.Columns.Count - 1; i++)
                {
                    if (setup.Missings != null && setup.Missings.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(e.Row.Cells[i].Column.UniqueName) && setup.Missings.ContainsKey(e.Row.Cells[i].Column.UniqueName))
                        {
                            SolidColorBrush background = new SolidColorBrush(Color.FromArgb(120, 216, 228, 188)); //Verde
                            int value = setup.Missings[e.Row.Cells[i].Column.UniqueName];

                            if (value > 0)
                            {
                                background = new SolidColorBrush(Color.FromArgb(120, 230, 184, 183)); //Rojo
                                e.Row.Cells[i].ToolTip = string.Format("Hay {0} SKUs sin {1} para esta categoría", value, e.Row.Cells[i].Column.UniqueName);
                            }
                            e.Row.Cells[i].Background = background;

                        }
                    }
                }
            }
        }

        private void SetupsGV_RowDetailsVisibilityChanged(object sender, GridViewRowDetailsEventArgs e)
        {
            RangeReviewViewModel vm = this.DataContext as RangeReviewViewModel;

            if (e.Visibility == Visibility.Visible)
            {
                RadGridView gv = e.DetailsElement as RadGridView;
                if (gv != null)
                {
                    if (vm.AttributeTypes != null)
                    {
                        int indexer = 0;
                        foreach (AttributeType type in vm.AttributeTypes.OrderBy(x => x.Id))
                        {
                            bool alreadyAdded = false;
                            foreach (Telerik.Windows.Controls.GridViewColumn c in gv.Columns)
                            {
                                if (c.Header.ToString() == type.Name)
                                {
                                    alreadyAdded = true;
                                    break;
                                }
                            }

                            if (alreadyAdded)
                                break;

                            GridViewCheckBoxColumn col = new GridViewCheckBoxColumn();
                            col.Header = type.Name;
                            col.UniqueName = type.Name;
                            col.AutoSelectOnEdit = true;
                            col.DataMemberBinding = new Binding(string.Format("Attributes[{0}].IsChecked", indexer++));

                            gv.Columns.Add(col);
                        }
                    }

                    //Sincronizo el ancho de las columnas con las del parent.
                    foreach (Telerik.Windows.Controls.GridViewColumn c in gv.Columns)
                    {
                        Telerik.Windows.Controls.GridViewColumn parentCol = SetupsGV.Columns.AsEnumerable<Telerik.Windows.Controls.GridViewColumn>().SingleOrDefault(x => x.UniqueName == c.UniqueName);
                        if (parentCol != null)
                        {
                            c.Width = parentCol.ActualWidth;
                        }
                    }
                }
            }
        }

        private void StoresGV_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            e.NewObject = new RangeReviewStore { Store = new Store { CountryId = WorkspaceData.Instance.Country.Id } };
        }

        private void CheckBox_Toggled(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;

            if (chk != null)
            {
                if (!chk.IsChecked.HasValue)
                    chk.IsChecked = false;

                (this.DataContext as RangeReviewViewModel).Setup_CheckAllCommand.Execute(chk);
            }
        }

    }
}
